package com.codename1.samples;


import com.codename1.charts.util.ColorUtil;
import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import static com.codename1.ui.ComponentSelector.$;
import com.codename1.ui.Container;
import com.codename1.ui.geom.Dimension;
import com.codename1.ui.layouts.FlowLayout;
import com.codename1.ui.layouts.LayeredLayout;
import java.util.Random;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class LayeredLayoutTest2751 {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Hi World", new LayeredLayout());
        //Form hi = new Form("Hi World", BoxLayout.y());
        Container[] components = new Container[20];
        for (int i=0; i<20; i++) {
            components[i] = dummyComponent(40, 20);
            
        }
        Container box = FlowLayout.encloseCenter(components);
        //box.setPreferredW(200);
        box.setWidth(200);
        Dimension prefSize = box.getPreferredSize();
        box.setPreferredH(prefSize.getHeight());
        box.setPreferredW(200);
        $(box).selectAllStyles()
                    .setBgTransparency(255)
                    .setBgColor(ColorUtil.YELLOW);
        hi.add(box);
        ((LayeredLayout)hi.getLayout()).setInsets(box, "auto auto auto auto");
        
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    private Container dummyComponent(int w, int h) {
        Container out = new Container();
        out.setPreferredW(w);
        out.setPreferredH(h);


        $(out)
                .selectAllStyles()
                .setBgTransparency(255)
                .setBgColor(randomColor())
                .setMargin(5);
        return out;
    }
    
    private int randomColor() {
        return ColorUtil.rgb(randomInt(), randomInt(), randomInt());
    }
    
    private int randomInt() {
        Random rnd = new Random();
        return rnd.nextInt(255);
    }
    
    public void destroy() {
    }

}
